package com.zzgele.qmzb.api.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Martin on 2017-07-16.
 * compute level
 */
public class LevelUtil {
    private final static Logger logger = LoggerFactory.getLogger(LevelUtil.class);
    public static List<Integer> LEVEL_SCORE=new ArrayList<>();

    public static int getNextLevelScore(int currentLevel,int score) {
        if(currentLevel<1)
            return 0;
        if(LEVEL_SCORE.size()<=currentLevel)
            return 0;
        return  LEVEL_SCORE.get(currentLevel)-score;
    }
    public static int getLevel(int score) {

        for(int i=1;i<LEVEL_SCORE.size();i++) {
             if(score>=LEVEL_SCORE.get(i-1)&&score<LEVEL_SCORE.get(i)){
                 return i;
             }else {
                 //满级
                 if(score>=LEVEL_SCORE.get(i)){
                     LEVEL_SCORE.size();
                 }
             }
        }
        return 1;
    }
      static    {
        BufferedReader reader = null;
        try {
            Resource resource = new ClassPathResource("classpath:level.txt");
            reader=new BufferedReader(new InputStreamReader(resource.getInputStream()));

            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                String temp[]=tempString.split("\t");
                LEVEL_SCORE.add(Integer.valueOf(temp[2]));
                line++;
            }
            reader.close();
        }catch (Exception e){
            logger.debug(e.getMessage());
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        
    }
}
